/* ************************************************************************
> File Name:     4.c
> Author:        zhfu
> mail:          3346897597@qq.com
> Created Time:  2022年05月03日 星期二 21时59分41秒
> Description:   
 ************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include"circlelinkedlist.h"

int main(int argc, char *argv[])
{
    int N = atoi(argv[1]);
    int M = atoi(argv[2]);

    if (N <= 0 || M <= 0){
        fputs("error!\n", stderr);
    }

    int i;
    for (i = 0; i < N; ++i){
        link p = make_node(i+1);
        insert(p);
    }

    link head = get_head();
    link p = head->next;
    while (length() != 1){
        int i = 1;
        while (i % M != 0){
            p = p->next;
            if (p == head){
                p = p->next;
            }
            ++i;
        }
        link q = p->next;
        delete(p);
        printf("kill NO.%d\n", p->item);
        p = q;
        if (p == head){
            p = p->next;
        }
    }

    printf("survivor: NO.%d\n", head->next->item);

    return 0;
}
